package com.yueka.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yueka.mapper.StudentMapper;
import com.yueka.pojo.PageResult;
import com.yueka.pojo.student.Student;
import com.yueka.pojo.student.StudentQueryParam;
import com.yueka.pojo.system_user.LoginInfo;
import com.yueka.service.StudentService;
import com.yueka.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageResult<Student> getStudentList(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> list = studentMapper.getStudentList(studentQueryParam);
        Page<Student> p = (Page<Student>) list;
        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    public void insertStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    @Override
    public List<Student> getInIds(Integer[] ids) {
        if (ids == null || ids.length == 0){
            return new ArrayList<>();
        }
        return studentMapper.getInIds(ids);
    }

    @Override
    @Transactional
    public LoginInfo login(Student student) {
        Student s = studentMapper.login(student);
        if (s!= null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", s.getId());
            claims.put("username", s.getStudentName());
            String token = JwtUtils.generateToken(s.getId().toString(), claims);
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setToken(token);
            loginInfo.setId(s.getId());
            loginInfo.setSystemUserName(s.getStudentName());
            loginInfo.setPhone(s.getPhone());
            return loginInfo;
        }
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.register(student);
        s = studentMapper.login(student);
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", s.getId());
        claims.put("username", s.getStudentName());
        String token = JwtUtils.generateToken(s.getId().toString(), claims);
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setToken(token);
        loginInfo.setId(s.getId());
        loginInfo.setSystemUserName(s.getStudentName());
        loginInfo.setPhone(s.getPhone());
        return loginInfo;
    }
}
